package net.yb.view;

import net.yb.constant.UserConstant;
import net.yb.entity.User;
import net.yb.service.UserService;
import net.yb.service.impl.UserServiceImpl;
import net.yb.util.JdbcUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Scanner;

public class Start {

    private static Scanner input = new Scanner(System.in);

    // 创建业务层对象
    private static UserService userService = new UserServiceImpl();

    public static void main(String[] args) {
        System.out.println("========================欢迎来到用户管理系统==========================");
        showMenu();
    }

    /**
     * 显示菜单
     */
    public static void showMenu(){
        while(true){
            System.out.println("-------------------------");
            System.out.println("1.添加用户");
            System.out.println("2.修改用户");
            System.out.println("3.删除用户");
            System.out.println("4.查询所有用户");
            System.out.println("5.根据id查询用户");
            System.out.println("6.根据多条件查询用户");
            System.out.println("7.退出");
            System.out.print("请输入您的选择：");
            int choice = input.nextInt();
            switch (choice){
                case 1:
                    addUser(); // 调用addUser()方法
                    break;
                case 2:
                    modifyUser();
                    break;
                case 3:
                    deleteUser();
                    break;
                case 4:
                    findAll();
                    break;
                case 5:
                    queryById();
                    break;
                case 6:
                    queryUser();
                    break;
                case 7:
                    System.out.println("谢谢使用");
                    System.exit(0); // 关闭程序
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
    }

    /**
     * 添加用户
     */
    public static void addUser(){
        System.out.println("-------------------------");
        User user = new User();
        System.out.print("请输入姓名：");
        user.setName(input.next());
        System.out.print("请输入手机号：");
        user.setPhone(input.next());
        System.out.print("请输入地址：");
        user.setAddress(input.next());
        user.setStatus(UserConstant.STATUS_ENABLE); // 默认启用

        try{
            userService.addUser(user);
            System.out.println("添加成功！");
        }catch(Exception e){
            System.out.println("添加失败：" + e.getMessage()); // 获取Service抛出的异常信息
        }
    }


    /**
     * 查询所有用户
     */
    public static void findAll(){
        System.out.println("-------------------------");
        List<User> list = userService.findAll();
        showUsers(list);
    }

    /**
     * 修改用户
     */
    public static void modifyUser(){
        System.out.println("-------------------------");
        System.out.print("请输入要修改的用户编号：");
        int id = input.nextInt();
        // 判断用户是否存在
        User user = userService.findById(id);
        if (user == null){
            System.out.println("提示：未找到id为" + id + "的用户！");
            return;
        }
        // 输入新的用户信息并修改
        System.out.print("请输入要修改的姓名：");
        user.setName(input.next());
        System.out.print("请输入要修改的手机号：");
        user.setPhone(input.next());
        System.out.print("请输入要修改的地址：");
        user.setAddress(input.next());
        try{
            userService.modify(user);
            System.out.println("修改成功！");
        }catch (Exception e){
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    public static void deleteUser(){
        System.out.println("-------------------------");
        System.out.print("请输入要删除的用户编号：");
        int id = input.nextInt();
        userService.removeById(id);
        System.out.println("删除成功！");
    }


    /**
     * 根据id查询用户
     */
    public static void queryById(){
        System.out.println("-------------------------");
        System.out.print("请输入要查询的用户编号：");
        int id = input.nextInt();
        User user = userService.findById(id);
        if (user == null){
            System.out.println("提示：未找到id为" + id + "的用户！");
        }else{
            System.out.println("编号\t\t姓名\t\t电话\t\t地址\t\t状态");
            System.out.println(user.getId() + "\t\t" + user.getName() + "\t\t" + user.getPhone() + "\t\t" + user.getAddress() + "\t\t" + user.getStatus());
        }
    }

    /**
     * 根据多条件查询用户
     */
    public static void queryUser(){ // user中包含查询条件
        System.out.println("-------------------------");
        System.out.print("请输入要查询的姓名：");
        String name = input.next();
        System.out.print("请输入要查询的地址：");
        String address = input.next();
        List<User> list = userService.findCondition(name, address);
        showUsers(list);
    }

    /**
     * 列表显示用户信息
     */
    public static void showUsers(List<User> list){
        System.out.println("编号\t\t姓名\t\t电话\t\t地址\t\t状态");
        for (User user : list){
            System.out.println(user.getId() + "\t\t" + user.getName() + "\t\t" + user.getPhone() + "\t\t" + user.getAddress() + "\t\t" + (user.getStatus()==0?"启用":"禁用"));
        }
    }
}
